home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 5 / MacMania 5.toast / / Internet software / NewsWatcher / NW Source / Source / child.c < prev    next >
Text File  |  1997-01-09  |  4KB  |  139 lines

  1. /*----------------------------------------------------------------------------
  2.  
  3.     child.c
  4.  
  5.     This module manages the parent/child window relationship
  6.     
  7.     Copyright © 1994-1997, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include "glob.h"
  12. #include "child.h"
  13. #include "memutil.h"
  14. #include "group.h"
  15.  
  16.  
  17.  
  18. /*----------------------------------------------------------------------------
  19.     AddChild 
  20.     
  21.     Add a child window to the child window list of a parent window.
  22.             
  23.     Entry:    parent = pointer to parent window.
  24.             child = pointer to child window.
  25.             
  26.     Exit:    function result = error code.
  27. ----------------------------------------------------------------------------*/
  28.  
  29. OSErr AddChild (WindowPtr parent, WindowPtr child)
  30. {
  31.     TWindow **parentInfo;
  32.     TChild **newChild;
  33.     OSErr err = noErr;
  34.     
  35.     parentInfo = (TWindow**) GetWRefCon(parent);
  36.     
  37.     err = MyNewHandle(sizeof(TChild), &newChild);
  38.     if (err != noErr) return err;
  39.     (**newChild).childWindow = child;
  40.     (**newChild).next = (**parentInfo).childList;
  41.     (**parentInfo).childList = newChild;
  42.     return noErr;
  43. }
  44.  
  45.  
  46.  
  47. /*----------------------------------------------------------------------------
  48.     RemoveChild 
  49.     
  50.     Remove a child window from the child window list of a parent window.
  51.             
  52.     Entry:    parent = pointer to parent window.
  53.             child = pointer to child window.
  54. ----------------------------------------------------------------------------*/
  55.  
  56. void RemoveChild (WindowPtr parent, WindowPtr child)
  57. {
  58.     TWindow **parentInfo;
  59.     TChild **current, **prev;
  60.     
  61.     if (parent == nil) return;
  62.     parentInfo = (TWindow**) GetWRefCon(parent);
  63.     for (current = prev = (**parentInfo).childList;
  64.         current != nil && (**current).childWindow != child;
  65.         prev = current, current = (**current).next) /* do nothing */;
  66.     if (current != nil) {
  67.         if (prev == current) {
  68.             (**parentInfo).childList = (**current).next;
  69.         } else {
  70.             (**prev).next = (**current).next;
  71.         }
  72.         MyDisposeHandle(current);
  73.     }
  74. }
  75.  
  76.  
  77.  
  78. /*----------------------------------------------------------------------------
  79.     FindChildByIndex 
  80.     
  81.     Locate a child window from an index in a group or subject array.
  82.             
  83.     Entry:    wind = pointer to group or subject window.
  84.             index = index in group or subject array.
  85.             
  86.     Exit:    function result = pointer to child window, or nil if none.
  87. ----------------------------------------------------------------------------*/
  88.     
  89. WindowPtr FindChildByIndex (WindowPtr wind, long index)
  90. {
  91.     TWindow **info, **childInfo;
  92.     TChild **childListRec;
  93.     WindowPtr child;
  94.     TWindowKind kind;
  95.     
  96.     info = (TWindow**)GetWRefCon(wind);
  97.     kind = (**info).kind;
  98.     for (childListRec = (**info).childList; childListRec != nil; 
  99.         childListRec = (**childListRec).next) 
  100.     {
  101.         child = (**childListRec).childWindow;
  102.         childInfo = (TWindow**)GetWRefCon(child);
  103.         if (kind == kSubject) {
  104.             if ((**childInfo).parentSubject == index) return child;
  105.         } else {
  106.             if ((**childInfo).parentGroup == index) return child;
  107.         }
  108.     }
  109.     return nil;
  110. }
  111.  
  112.  
  113.  
  114. /*----------------------------------------------------------------------------
  115.     FindChild 
  116.     
  117.     Locate a child window corresponding to an item in a group or subject 
  118.     window list.
  119.             
  120.     Entry:    wind = pointer to group or subject window.
  121.             item = item number in the window list.
  122.             
  123.     Exit:    function result = pointer to child window, or nil if none.
  124. ----------------------------------------------------------------------------*/
  125.     
  126. WindowPtr FindChild (WindowPtr wind, long item)
  127. {
  128.     TWindow **info;
  129.     long index;
  130.     
  131.     info = (TWindow**)GetWRefCon(wind);
  132.     if ((**info).kind == kGroup) {
  133.         index = BigLGetData((**info).groupList, item);
  134.     } else {
  135.         index = BigLGetData((**info).subjectList, item);
  136.     }
  137.     return FindChildByIndex(wind, index);
  138. }
  139.